//
//  ViewController.swift
//  PlayWithXMPP_Swift
//
//  Created by admin on 15/1/27.
//  Copyright (c) 2015年 com.zhongyan. All rights reserved.
//

import UIKit

class ViewController: UIViewController, XMPPStreamDelegate, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupTableView()
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        if UserInfoHelper.isLogin()
        {
            println("已经登陆")
            self.setupXMPPStream()
            self.connect()
            
            
        }
        else
        {
            println("未登陆")
            self.presentLoginVC()
        }
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        AppDelegate.shareInstance().xmppStream!.removeDelegate(self)
    }
    
    func setupTableView() {
        self.tableView!.delegate = self;
        self.tableView!.dataSource = self;
        self.tableView!.separatorInset = UIEdgeInsetsMake(0, 70, 0, 0)
    }
    
    
    
    func setupXMPPStream() {
        let userName = UserInfoHelper.getUserName()
        let password = UserInfoHelper.getPassword()
        AppDelegate.shareInstance().xmppStream!.myJID = XMPPJID.jidWithString(userName)
        AppDelegate.shareInstance().xmppStream!.hostName = "10.10.1.114"
        AppDelegate.shareInstance().xmppStream!.addDelegate(self, delegateQueue: dispatch_get_main_queue())
        
    }
    
    func connect() {
        AppDelegate.shareInstance().xmppStream!.connectWithTimeout(XMPPStreamTimeoutNone, error: nil)
    }
    func presentLoginVC() {
        let loginVC:LoginViewController = AppDelegate.getVCFromSB("loginVC") as LoginViewController
        self.presentViewController(loginVC, animated: true) { () -> Void in
        }
    }
    
    
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:ContactPersonCell = tableView.dequeueReusableCellWithIdentifier("contactPersonCell", forIndexPath: indexPath) as ContactPersonCell
        return cell
    }
    
    func xmppStreamDidConnect(sender: XMPPStream!) {
        println(__FUNCTION__, __LINE__);
        let password = UserInfoHelper.getPassword()
        AppDelegate.shareInstance().xmppStream!.authenticateWithPassword(password, error: nil)
    }
    
    func xmppStream(sender: XMPPStream!, didSendMessage message: XMPPMessage!) {
        println(__FUNCTION__, __LINE__);
    }
    
    func xmppStream(sender: XMPPStream!, didReceivePresence presence: XMPPPresence!) {
        println(__FUNCTION__, __LINE__);
    }
    func xmppStream(sender: XMPPStream!, didReceiveMessage message: XMPPMessage!) {
        println(__FUNCTION__, __LINE__);
        println(message)
    }
    
    func xmppStream(sender: XMPPStream!, didNotAuthenticate error: DDXMLElement!) {

    }
    
    func xmppStreamDidAuthenticate(sender: XMPPStream!) {
        println(__FUNCTION__, __LINE__);
        let presence:XMPPPresence = XMPPPresence()
        AppDelegate.shareInstance().xmppStream?.sendElement(presence)
  
       

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }


}

